package com.jzo2o.foundations.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.jzo2o.foundations.constants.RedisConstants;
import com.jzo2o.foundations.enums.FoundationStatusEnum;
import com.jzo2o.foundations.mapper.ServeMapper;
import com.jzo2o.foundations.model.domain.Region;
import com.jzo2o.foundations.model.dto.response.HotServeVO;
import com.jzo2o.foundations.model.dto.response.ServeCategoryResDTO;
import com.jzo2o.foundations.model.dto.response.ServeTypeResDTO;
import com.jzo2o.foundations.service.HomeService;
import com.jzo2o.foundations.service.IRegionService;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author itlxc
 * @since 2024-10-16
 */
@Service
@RequiredArgsConstructor
public class HomeServiceImpl implements HomeService {

    private final ServeMapper serveMapper;

    private final IRegionService regionService;


    /**
     * 根据区域id获取服务图标信息
     *
     * @param regionId 区域id
     * @return 服务图标列表
     */
    @Override
    @Caching(cacheable = {
            // result为null时,属于缓存穿透情况，缓存时间30分钟
            @Cacheable(value = RedisConstants.CacheName.SERVE_ICON, key = "#regionId" ,
                    unless = "#result.size() != 0 ",
                    cacheManager = RedisConstants.CacheManager.THIRTY_MINUTES),
            // result不为null时,永久缓存
            @Cacheable(value = RedisConstants.CacheName.SERVE_ICON, key = "#regionId",
                    unless = "#result.size() == 0",
                    cacheManager = RedisConstants.CacheManager.FOREVER)
    })
    public List<ServeCategoryResDTO> queryServeIconCategoryByRegionIdCache(Long regionId) {
        // 1.校验当前城市是否为启用状态
        Region region = regionService.getById(regionId);
        if (ObjectUtil.isNull(region) || region.getActiveStatus() == FoundationStatusEnum.DISABLE.getStatus()) {
            return Collections.emptyList();
        }
        // 2.根据城市编码查询所有的服务图标
        List<ServeCategoryResDTO> list = serveMapper.findServeIconCategoryByRegionId(regionId);
        if (CollUtil.isEmpty(list)) {
            return Collections.emptyList();
        }
        // 3.服务类型取前两个，每个类型下服务项取前4个
        list = list.stream().limit(2).collect(Collectors.toList());
        list.forEach(item -> item.setServeResDTOList(item.getServeResDTOList().stream().limit(4).collect(Collectors.toList())));
        return list;
    }

    /**
     * 查询服务类型列表
     * @return
     */
    @Override
    @Cacheable(value = RedisConstants.CacheName.SERVE_TYPE, key = "#regionId", cacheManager = RedisConstants.CacheManager.FOREVER)
    public List<ServeCategoryResDTO> queryServeTypeList(Long regionId) {
        return serveMapper.getServeTypeListByRegionId(regionId);
    }

    /**
     * 查询热门服务列表
     * @param regionId
     * @return
     */
    @Override
    @Caching(cacheable = {
            // result不为null时,永久缓存
            @Cacheable(value = RedisConstants.CacheName.HOT_SERVE,
                    key = "#regionId", unless = "#result.size() == 0",
                    cacheManager = RedisConstants.CacheManager.FOREVER),
            // result为null时,属于缓存穿透情况，缓存时间30分钟
            @Cacheable(value = RedisConstants.CacheName.HOT_SERVE,
                    key = "#regionId", unless = "#result.size() != 0",
                    cacheManager = RedisConstants.CacheManager.THIRTY_MINUTES)
    })
    public List<HotServeVO> queryHotServeList(Long regionId) {
        return serveMapper.getHotServeList(regionId);
    }

    /**
     * 查看服务详情
     * @param id
     * @return
     */
    @Override
    @Cacheable(value = RedisConstants.CacheName.SERVE,
            key = "#id", cacheManager = RedisConstants.CacheManager.ONE_DAY)
    public HotServeVO getServeDetail(Long id) {
        return serveMapper.getServeDetail(id);
    }
}
